package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.AddCourseTeacherDto;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 摆渡人
 * @description
 * @date 2023/7/14 19:52
 */
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper,CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Override
    public List<CourseTeacher> listTeacher(Long courseId) {
        return this.lambdaQuery().eq(CourseTeacher::getCourseId,courseId).list();
    }

    @Override
    public CourseTeacher addTeacher(AddCourseTeacherDto addCourseTeacherDto) {
        Long id = addCourseTeacherDto.getId();
        if(id == null) {
            CourseTeacher courseTeacher = new CourseTeacher();
            BeanUtils.copyProperties(addCourseTeacherDto,courseTeacher);
            courseTeacher.setCreateDate(LocalDateTime.now());
            this.save(courseTeacher);
            return courseTeacher;
        } else {
            CourseTeacher teacher = this.getById(id);
            if(teacher == null) {
                throw new XueChengPlusException("数据不存在");
            }
            BeanUtils.copyProperties(addCourseTeacherDto,teacher);
            this.updateById(teacher);
            return teacher;
        }
    }

    @Override
    public void deleteTeacher(Long courseId, Long teacherId) {
        this.lambdaUpdate().eq(CourseTeacher::getCourseId,courseId).eq(CourseTeacher::getId,teacherId).remove();
    }
}
